//
// Created by 28735 on 2024/11/25.
//

#ifndef SWAPSORT_H
#define SWAPSORT_H

/*  交换排序分为冒泡排序和快排*/

inline void bubbleSort(int arr[])
{
    int length = sizeof(arr) / sizeof(int);

    for (int i = 0; i < length; i++)
    {
        for (int j = length - 1; j > i; j--)
        {
            if (arr[j] < arr[j - 1])
            {
                int temp = arr[j];
                arr[j] = arr[j - 1];
                arr[j - 1] = temp;
            }
        }
    }
}


inline int quickSortImpl(int arr[], int low, int high)
{
    int mid = arr[low];
    while (low < high)
    {
        while (arr[high] >= mid && high > low)
        {
            high--;
        }

        arr[low] = arr[high];

        while (arr[low] <= mid && low < high)
        {
            low++;
        }
        arr[high] = arr[low];
    }
    arr[low] = mid;
    return low;
}

inline void quickSortRecursion(int arr[], int low, int high)
{
    int mid = quickSortImpl(arr, low, high);
    quickSortRecursion(arr, mid + 1, high);
    quickSortRecursion(arr, low, mid - 1);
}

inline void quickSort(int arr[])
{
    int length = sizeof(arr) / sizeof(int);
    quickSortRecursion(arr, 0, length - 1);
}


#endif //SWAPSORT_H
